﻿using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Bill.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.ComponentModel;
using static System.Net.WebRequestMethods;
using Kingdee.BOS;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.JSON;
using System.Data.SqlClient;
using Kingdee.BOS.Core.DynamicForm;
using System.Collections.Generic;
using Kingdee.BOS.App.Data;


using System.Data;
using System.Text;
using Kingdee.BOS.Log;
using Kingdee.BOS.Cache;
using Kingdee.BOS.Core.Objects.WeiXin;


using System.Linq;
using Kingdee.BOS.Core.Metadata;
// 引用 Kingdee.BOS.ServiceHelper.dll
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.List.PlugIn;
using Newtonsoft.Json.Linq;
using static Kingdee.BOS.App.Core.Validation.EntryEntity.CompositePKValidator.KeyHelper;
using Kingdee.BOS.Core.Msg;
using System.Collections;
using Kingdee.BOS.Core.List;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using Kingdee.BOS.Core.Enums;
using Kingdee.BOS.Core.Metadata.Operation;
using Kingdee.BOS.Core.Objects.BillCodeRule;
using System.Xml.Linq;
using Kingdee.BOS.WebApi.ServicesStub;
using Kingdee.BOS.WebApi.FormService;
using Kingdee.BOS.Core.CommonFilter;
namespace BeforeControlDataBinderFormPlugIn
{
    [Description("【获取和返回单据】BeforeControlDataBinder"), HotUpdate]
    public class AA : AbstractDynamicFormPlugIn
    {
        private readonly string connectionString = "Server=192.168.10.5,1433;Database=JW_HS;User Id=HSZPY;Password=hszpy_1234;";

        public override void AfterCreateNewData(EventArgs e)
        {
            base.AfterCreateNewData(e);

            string query = @"
                /*dialect*/
                SELECT 
                FContractTypeID,
                FContractCodes,
                FCustomerID,
                FCustomerCodes,
                FCustomerNames,
                FProductEntryID
                FROM (
                SELECT 
                ROW_NUMBER() OVER (PARTITION BY a.FContractCodes ORDER BY a.FProductEntryID) AS RowNum,
                d1.FContractTypeID,
                a.FContractCodes,
                b.FCustomerID,
                a.FCustomerCodes,
                a.FCustomerNames,
                a.FProductEntryID,
                b.FDepotID,
                b.FMaterialQualityID
                FROM 
                St_ProductEntry a 
                INNER JOIN 
                St_ProductEntryDe b ON a.FProductEntryID = b.FProductEntryID
                LEFT JOIN 
                Fm_Use c ON c.FUseID = b.FUseID
                INNER JOIN 
                So_ContractDe d ON d.FContractDeID = b.FContractDeID --销售合同
                INNER JOIN 
                So_Contract d1 ON d1.FContractID = d.FContractID --合同主表
                INNER JOIN
                JW_HS.dbo.So_Order AS x1 ON x1.FOrderID = d.FOrderID --销售订单表
                LEFT JOIN 
                Bd_ProductReview e ON e.FProductReviewID = d.FProductReviewID --评审
                LEFT JOIN 
                JW_HS.dbo.Mf_product AS f ON f.FProductID = e.FProductID --成品
                INNER JOIN 
                JW_HS.dbo.Hr_Employee AS h ON h.FEmplID = d1.FEmplID --业务员
                LEFT JOIN 		
                St_Depot AS i ON i.FDepotID = b.FDepotID --库位
                LEFT JOIN
                Mf_WorkShip j ON j.FWorkShipID = b.FWorkShipID --仓库
                LEFT JOIN 
                JW_HS.dbo.Fm_MaterialQuality AS k ON k.FMaterialQualityID = b.FMaterialQualityID --材质	
                LEFT JOIN (
                SELECT 
                x.FProductReviewID AS 评审ID,
                SUM(CASE x.FProjectName WHEN '最小卷径' THEN x.FPrecise ELSE 0 END) AS FPreciseUpper,
                SUM(CASE x.FProjectName WHEN '最大卷径' THEN x.FPrecise ELSE 0 END) AS FPreciseLower,
                SUM(CASE x.FProjectName WHEN '单拖重下限(kg)' THEN x.FPrecise ELSE 0 END) AS FPreciseWeightUpper,
                SUM(CASE x.FProjectName WHEN '单拖重上限(kg)' THEN x.FPrecise ELSE 0 END) AS FPreciseWeightLower
                FROM (
                SELECT 
                b.FProjectName,
                a.FProductReviewID,
                a.FPrecise
                FROM 
                (SELECT * FROM dbo.Bd_ProductReviewDe WHERE FProjectID IN (3, 4, 5, 6)) AS a
                INNER JOIN 
                dbo.Bd_Project AS b ON b.FProjectID = a.FProjectID
                WHERE 1=1
                ) AS x
                GROUP BY 
                x.FProductReviewID
                ) AS y ON y.评审ID = d.FProductReviewID
                WHERE 1=1

                ";



            //销售合同编号
            string FSLCONTRACT = ObjectUtils.Object2String(this.View.Model.GetValue(" FSLCONTRACT"));
            if (!string.IsNullOrEmpty(FSLCONTRACT))
            {
                query += $" AND a.FContractCodes = '{FSLCONTRACT.Replace("'", "''")}'";
            }
            //客户编号  
            string FKHCODE = ObjectUtils.Object2String(this.View.Model.GetValue(" FKHCODE"));
            if (!string.IsNullOrEmpty(FKHCODE))
            {
                query += $" AND  a.FCustomerCodes = '{FKHCODE.Replace("'", "''")}'";
            }
            //客户名称  
            string FKHNAME = ObjectUtils.Object2String(this.View.Model.GetValue(" FKHNAME"));
            if (!string.IsNullOrEmpty(FKHNAME))
            {
                query += $" AND  a.FCustomerNames like  '%{FKHNAME.Replace("'", "''")}%'";
            }

            string FApproveDate_Begin = ObjectUtils.Object2String(this.View.Model.GetValue("FApproveDate_Begin"));
            string FApproveDate_End = ObjectUtils.Object2String(this.View.Model.GetValue("FApproveDate_End"));


            if (!string.IsNullOrEmpty(FApproveDate_Begin))
            {
                query += $" AND a.FApproveDate >= '{DateTime.Parse(FApproveDate_Begin):yyyy-MM-dd HH:mm:ss}'";

            }
            if (!string.IsNullOrEmpty(FApproveDate_End))
            {
                query += $" AND  a.FApproveDate <= '{DateTime.Parse(FApproveDate_End).AddDays(1).AddSeconds(-1):yyyy-MM-dd HH:mm:ss}'";

            }


            if (string.IsNullOrEmpty(FApproveDate_Begin) && string.IsNullOrEmpty(FApproveDate_End))
            {
                DateTime todayStart = DateTime.Today;
                DateTime todayEnd = todayStart.AddDays(1).AddTicks(-1);
                query += $" AND  a.FApproveDate >= '{todayStart:yyyy-MM-dd HH:mm:ss}'";
                query += $" AND a.FApproveDate <= '{todayEnd:yyyy-MM-dd HH:mm:ss}'";
            }


            query += @"
                ) AS DistinctContracts
                WHERE RowNum = 1;";


            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                try
                {
                    connection.Open();
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            // 示例：只读取前几行并展示结果
                            int count = 0;

                            while (reader.Read())

                            {
                                this.Model.CreateNewEntryRow("F_UNW_Entity_83g");
                                this.Model.SetValue("FHTBH", ObjectUtils.Object2String(reader["FContractCodes"]), count);//合同编号
                                this.Model.SetValue("FKHID", ObjectUtils.Object2String(reader["FCustomerID"]), count);//客户id
                                this.Model.SetValue("FKHBH", ObjectUtils.Object2String(reader["FCustomerCodes"]), count);//客户编号
                                this.Model.SetValue("FKHMC", ObjectUtils.Object2String(reader["FCustomerNames"]), count);//客户名称
                                this.Model.SetValue("FMID", ObjectUtils.Object2String(reader["FProductEntryID"]), count);//主表id

                                count++;
                            }

                        }
                    }
                }
                catch (Exception ex)
                {
                    // 显示错误信息
                    this.View.ShowMessage("数据库访问出错: " + ex.Message);
                }
            }
        }
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);
            if (e.BarItemKey.Equals("tbSearch"))
            {
                Entity entity2 = this.View.BusinessInfo.GetEntity("FEntity");
                DynamicObjectCollection entityDataObjects3 = this.View.Model.GetEntityDataObject(entity2);


                // 记录要删除的行索引
                List<int> rowsToDelete2 = new List<int>();

                for (int j = 0; j < entityDataObjects3.Count; j++)
                {
                    //DynamicObject entityDataObject = entityDataObjects2[j];
                    rowsToDelete2.Add(j);
                    //if (entityDataObject["F_FCustomerID"] == null)
                    //{
                    //    rowsToDelete.Add(j); // 将需要删除的行索引添加到列表
                    //}
                }

                for (int i = rowsToDelete2.Count - 1; i >= 0; i--)
                {
                    this.View.Model.DeleteEntryRow("FEntity", rowsToDelete2[i]);
                }



                Entity entity1 = this.View.BusinessInfo.GetEntity("F_UNW_Entity_83g");
                DynamicObjectCollection entityDataObjects2 = this.View.Model.GetEntityDataObject(entity1);


                // 记录要删除的行索引
                List<int> rowsToDelete1 = new List<int>();

                for (int j = 0; j < entityDataObjects2.Count; j++)
                {
                    //DynamicObject entityDataObject = entityDataObjects2[j];
                    rowsToDelete1.Add(j);
                    //if (entityDataObject["F_FCustomerID"] == null)
                    //{
                    //    rowsToDelete.Add(j); // 将需要删除的行索引添加到列表
                    //}
                }

                for (int i = rowsToDelete1.Count - 1; i >= 0; i--)
                {
                    this.View.Model.DeleteEntryRow("F_UNW_Entity_83g", rowsToDelete1[i]);
                }

                string query = @"
                /*dialect*/
                SELECT 
                FContractTypeID,
                FContractCodes,
                FCustomerID,
                FCustomerCodes,
                FCustomerNames,
                FProductEntryID
                FROM (
                SELECT 
                ROW_NUMBER() OVER (PARTITION BY a.FContractCodes ORDER BY a.FProductEntryID) AS RowNum,
                d1.FContractTypeID,
                a.FContractCodes,
                b.FCustomerID,
                a.FCustomerCodes,
                a.FCustomerNames,
                a.FProductEntryID,
                b.FDepotID,
                b.FMaterialQualityID
                FROM 
                St_ProductEntry a 
                INNER JOIN 
                St_ProductEntryDe b ON a.FProductEntryID = b.FProductEntryID
                LEFT JOIN 
                Fm_Use c ON c.FUseID = b.FUseID
                INNER JOIN 
                So_ContractDe d ON d.FContractDeID = b.FContractDeID --销售合同
                INNER JOIN 
                So_Contract d1 ON d1.FContractID = d.FContractID --合同主表
                INNER JOIN
                JW_HS.dbo.So_Order AS x1 ON x1.FOrderID = d.FOrderID --销售订单表
                LEFT JOIN 
                Bd_ProductReview e ON e.FProductReviewID = d.FProductReviewID --评审
                LEFT JOIN 
                JW_HS.dbo.Mf_product AS f ON f.FProductID = e.FProductID --成品
                INNER JOIN 
                JW_HS.dbo.Hr_Employee AS h ON h.FEmplID = d1.FEmplID --业务员
                LEFT JOIN 		
                St_Depot AS i ON i.FDepotID = b.FDepotID --库位
                LEFT JOIN
                Mf_WorkShip j ON j.FWorkShipID = b.FWorkShipID --仓库
                LEFT JOIN 
                JW_HS.dbo.Fm_MaterialQuality AS k ON k.FMaterialQualityID = b.FMaterialQualityID --材质	
                LEFT JOIN (
                SELECT 
                x.FProductReviewID AS 评审ID,
                SUM(CASE x.FProjectName WHEN '最小卷径' THEN x.FPrecise ELSE 0 END) AS FPreciseUpper,
                SUM(CASE x.FProjectName WHEN '最大卷径' THEN x.FPrecise ELSE 0 END) AS FPreciseLower,
                SUM(CASE x.FProjectName WHEN '单拖重下限(kg)' THEN x.FPrecise ELSE 0 END) AS FPreciseWeightUpper,
                SUM(CASE x.FProjectName WHEN '单拖重上限(kg)' THEN x.FPrecise ELSE 0 END) AS FPreciseWeightLower
                FROM (
                SELECT 
                b.FProjectName,
                a.FProductReviewID,
                a.FPrecise
                FROM 
                (SELECT * FROM dbo.Bd_ProductReviewDe WHERE FProjectID IN (3, 4, 5, 6)) AS a
                INNER JOIN 
                dbo.Bd_Project AS b ON b.FProjectID = a.FProjectID
                WHERE 1=1
                ) AS x
                GROUP BY 
                x.FProductReviewID
                ) AS y ON y.评审ID = d.FProductReviewID
                WHERE 1=1
                ";



                //销售合同编号
                string FSLCONTRACT = ObjectUtils.Object2String(this.View.Model.GetValue(" FSLCONTRACT"));
                if (!string.IsNullOrEmpty(FSLCONTRACT))
                {
                    query += $" AND a.FContractCodes = '{FSLCONTRACT.Replace("'", "''")}'";
                }
                //客户编号  
                string FKHCODE = ObjectUtils.Object2String(this.View.Model.GetValue(" FKHCODE"));
                if (!string.IsNullOrEmpty(FKHCODE))
                {
                    query += $" AND  a.FCustomerCodes = '{FKHCODE.Replace("'", "''")}'";
                }
                //客户名称  
                string FKHNAME = ObjectUtils.Object2String(this.View.Model.GetValue(" FKHNAME"));
                if (!string.IsNullOrEmpty(FKHNAME))
                {
                    query += $" AND  a.FCustomerNames like '%{FKHNAME.Replace("'", "''")}%'";
                }

                string FApproveDate_Begin = ObjectUtils.Object2String(this.View.Model.GetValue("FApproveDate_Begin"));
                string FApproveDate_End = ObjectUtils.Object2String(this.View.Model.GetValue("FApproveDate_End"));


                if (!string.IsNullOrEmpty(FApproveDate_Begin))
                {
                    query += $" AND a.FApproveDate >= '{DateTime.Parse(FApproveDate_Begin):yyyy-MM-dd HH:mm:ss}'";

                }
                if (!string.IsNullOrEmpty(FApproveDate_End))
                {
                    query += $" AND  a.FApproveDate <= '{DateTime.Parse(FApproveDate_End).AddDays(1).AddSeconds(-1):yyyy-MM-dd HH:mm:ss}'";

                }


                if (string.IsNullOrEmpty(FApproveDate_Begin) && string.IsNullOrEmpty(FApproveDate_End))
                {
                    DateTime todayStart = DateTime.Today;
                    DateTime todayEnd = todayStart.AddDays(1).AddTicks(-1);
                    query += $" AND  a.FApproveDate >= '{todayStart:yyyy-MM-dd HH:mm:ss}'";
                    query += $" AND a.FApproveDate <= '{todayEnd:yyyy-MM-dd HH:mm:ss}'";
                }


                query += @"
                ) AS DistinctContracts
                WHERE RowNum = 1;";


                //this.View.ShowMessage(query);
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    try
                    {
                        connection.Open();
                        using (SqlCommand command = new SqlCommand(query, connection))
                        {
                            using (SqlDataReader reader = command.ExecuteReader())
                            {
                                // 示例：只读取前几行并展示结果
                                int count = 0;

                                while (reader.Read())

                                {
                                    // 创建新行
                                    this.Model.CreateNewEntryRow("F_UNW_Entity_83g");
                                    this.Model.SetValue("FHTBH", ObjectUtils.Object2String(reader["FContractCodes"]), count);//合同编号
                                    this.Model.SetValue("FKHID", ObjectUtils.Object2String(reader["FCustomerID"]), count);//客户id
                                    this.Model.SetValue("FKHBH", ObjectUtils.Object2String(reader["FCustomerCodes"]), count);//客户编号
                                    this.Model.SetValue("FKHMC", ObjectUtils.Object2String(reader["FCustomerNames"]), count);//客户名称
                                    this.Model.SetValue("FMID", ObjectUtils.Object2String(reader["FProductEntryID"]), count);//主表id



                                    count++;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        // 显示错误信息
                        this.View.ShowMessage("数据库访问出错: " + ex.Message);
                    }

                }

            }
            if (e.BarItemKey.Equals("tbReturnData"))
            {
                int count = 0;
                //获取单据体
                Entity entity = this.View.BillBusinessInfo.GetEntity("FEntity");
                //转换为一行一行的数据
                DynamicObjectCollection entityDataObject = this.View.Model.GetEntityDataObject(entity);

                //定义接收
                DynamicObjectCollection dymat = new DynamicObjectCollection(entity.DynamicObjectType, null);
                foreach (DynamicObject current in entityDataObject)
                {
                    dymat.Add(current);
                    count++;
                    //if (current["F_CheckBox"].ToString().ToUpper() == "TRUE")
                    //{

                    //}
                }
                if (count == 0)
                {
                    this.View.ShowMessage("请选择数据行");
                    return;
                }
                //if (count > 0)
                //{
                //    this.View.ShowMessage("只能选择一行");
                //    return;
                //}
                if (count >= 1)
                {
                    //返回主表界面
                    FormResult rormResult = new FormResult(dymat);
                    this.View.ReturnToParentWindow(rormResult);
                    this.View.Close();
                }
            }
        }

    }
}
namespace PanelShowFormPlugIn

{

    [Description("行点击事件"), HotUpdate]

    public class EntityRowDoubleClickData : AbstractDynamicFormPlugIn

    {

        public override void EntityRowDoubleClick(EntityRowClickEventArgs e)

        {
            base.EntityRowDoubleClick(e);
            Entity entity1 = this.View.BusinessInfo.GetEntity("FEntity");
            DynamicObjectCollection entityDataObjects2 = this.View.Model.GetEntityDataObject(entity1);


            // 记录要删除的行索引
            List<int> rowsToDelete1 = new List<int>();

            for (int j = 0; j < entityDataObjects2.Count; j++)
            {
                //DynamicObject entityDataObject1 = entityDataObjects2[j];
                rowsToDelete1.Add(j);
                //if (entityDataObject["F_FCustomerID"] == null)
                //{
                //    rowsToDelete.Add(j); // 将需要删除的行索引添加到列表
                //}
            }

            for (int i = rowsToDelete1.Count - 1; i >= 0; i--)
            {
                this.View.Model.DeleteEntryRow("FEntity", rowsToDelete1[i]);
            }



            Entity entity = this.View.BillBusinessInfo.GetEntity("F_UNW_Entity_83g");
            //转换为一行一行的数据
            DynamicObjectCollection entityDataObject = this.View.Model.GetEntityDataObject(entity);
            //string FHTBH = ObjectUtils.Object2String(entityDataObject[e.Row]["FHTBH"]);
            //string FKHID = ObjectUtils.Object2String(entityDataObject[e.Row]["FKHID"]);
            //string FKHBH = ObjectUtils.Object2String(entityDataObject[e.Row]["FKHBH"]);
            //string FKHMC = ObjectUtils.Object2String(entityDataObject[e.Row]["FKHMC"]);
            string FMID = ObjectUtils.Object2String(entityDataObject[e.Row]["FMID"]);
            string connectionString = "Server=192.168.10.5,1433;Database=JW_HS;User Id=HSZPY;Password=hszpy_1234;";
            string query = $@"
            SELECT 
            d1.FContractTypeID,--合同类型
            d.FOrderID, --销售订单id
            x1.FBillCode , --销售订单编号
            a.FWarrantyName,--质保书号
            b.FWorktaskManageID,--生产任务单号
            b.FProApplicationDeID,--生产入库申请单id
            a.FProductEntryCode, --入库单号
            d.FProductReviewID, --评审id
            d.FCode,--评审编号
            a.FProductEntryID,--主表id
            b.FLCCard,--卡号
            -- a.FCodes,--卡号
            b.FStatus,--出库状态  --未出库则为NULL
            b.FYCNum,--异常数量
            b.FLJRKNum,--累计入库数量
            b.FDPNum,--单批数量
            b.FUseID,--用途id
            c.FUseName, --用途
            c.FUseCode, --用途编码
            b.FBatchNo,--批次号
            a.FTol,--批号
            b.FTotNo,--托码
            a.FTolNum,--托码
            b.FTotN,--托数
            b.FTotNum,--单拖重量(kg)
            b.FActualThickness,--实际厚度(mm)
            b.FActualWidth,--实际宽度(mm)
            b.FOrderNum,--订单数量
            b.FStandardYield,--标准成材率
            b.FActualYield,--实际成材率
            b.FApplyNum,--入库申请数量(kg)
            b.FProductNum,--标准成品数量
            b.FDepotID,--库位
            i.FDepotCode,--库位编号
            i.FDepotName,--库位名称
            j.FWorkShipName,--仓库名称
            b.FRollNum,--卷数
            b.FRemark AS FRemark_s,--明细备注
            b.FWorkShipID, --仓库
            b.FContractDeID,--合同id
            a.FContractCodes, --合同编号
            -- b.FProductID,--物料编码
            f.FProductID , -- 成品
            f.FProductCode,
            f.FProductName,
            f.FProductSpec,--成品规格
            b.FMaterialQualityID,--材质id
            a.FMaterialQualityNames, --材质
            k.FMaterialQualityCode, --材质编码
            b.FUnitID,--单位
            h.FEmplID,-- 业务员ID
            h.FEmplCode , --业务员编号
            h.FEmplName, --业务员名称
            e.FThickness, --厚度
            e.FThicknessUpper,
            e.FThicknessLower,
            e.FWidth, --宽度
            e.FWidthUpper,
            e.FWidthLower,
            e.FLength, --长度
            e.FLengthUpper,
            e.FLengthLower,
            e.FTargetThickness,--目标厚度
		
		
            b.FWoodenModeID,--木架id
            a.FWoodenModeNames,--木架名称
            a.FDate, --入库日期
            a.FSumNum,--总成品数量
            a.FRollNums,--卷数汇总
            a.FTotNums,--托数
            a.FWorkShipID AS FWorkShipID_S,--仓库 
            a.FRemark,--备注
            b.FCustomerID,--客户id
            a.FCustomerCodes, --客户编号
            a.FCustomerNames, --客户名称
            a.FJHDates,--交货日期
            a.FMaterialQualityDeNames, --客方材质
            a.FCreateDate,--创建日期
            a.FCreater,--创建人
            a.FOperDate,--操作时间
            a.FOperUser,--操作人
            a.FApprove, --审核状态
            a.FApproveDate,--审核时间
            a.FApprover, --审核人
            a.FDepaID,--制单部门
            a.FApproveDepaID, --审核部门
            a.FIsApproval,--需要签批
            a.FApprovalStauts,--签批状态
            a.FApprovalDate,--签批时间
            a.FApplyEmplID, --签批人
            a.FEnable, --是否启用
            -- y.评审ID,
            y.FPreciseUpper,--最小卷径
            y.FPreciseLower,--最大卷径
            y.FPreciseWeightUpper,--单拖重下限_kg,
            y.FPreciseWeightLower--单拖重上限_kg

            FROM 
            St_ProductEntry a 
            INNER JOIN 
            St_ProductEntryDe b ON a.FProductEntryID = b.FProductEntryID
            LEFT JOIN 
            Fm_Use c ON c.FUseID = b.FUseID
            INNER JOIN 
            So_ContractDe d ON d.FContractDeID = b.FContractDeID--销售合同
            INNER JOIN 
            So_Contract d1 ON d1.FContractID = d.FContractID --合同主表

            INNER JOIN
            JW_HS.dbo.So_Order AS x1 ON x1.FOrderID = d.FOrderID --销售订单表

            LEFT JOIN 
            Bd_ProductReview e ON e.FProductReviewID = d.FProductReviewID  --评审
            LEFT JOIN 
            JW_HS.dbo.Mf_product AS f ON f.FProductID = e.FProductID --成品
            INNER JOIN 
            JW_HS.dbo.Hr_Employee AS h ON h.FEmplID = d1.FEmplID --业务员
            LEFT JOIN 		
            St_Depot AS i ON i.FDepotID = b.FDepotID --库位
            LEFT JOIN
            Mf_WorkShip j ON j.FWorkShipID = b.FWorkShipID --仓库
            LEFT JOIN 
            JW_HS.dbo.Fm_MaterialQuality AS k ON k.FMaterialQualityID = b.FMaterialQualityID--材质	
            -- 将第二个查询作为子查询加入
            LEFT JOIN (
            SELECT 
            x.FProductReviewID AS 评审ID,
            SUM(CASE x.FProjectName WHEN '最小卷径' THEN x.FPrecise ELSE 0 END) AS FPreciseUpper,
            SUM(CASE x.FProjectName WHEN '最大卷径' THEN x.FPrecise ELSE 0 END) AS FPreciseLower,
            SUM(CASE x.FProjectName WHEN '单拖重下限(kg)' THEN x.FPrecise ELSE 0 END) AS FPreciseWeightUpper,
            SUM(CASE x.FProjectName WHEN '单拖重上限(kg)' THEN x.FPrecise ELSE 0 END) AS FPreciseWeightLower
            FROM (
            SELECT 
            b.FProjectName,
            a.FProductReviewID,
            a.FPrecise
            FROM 
            (SELECT * FROM dbo.Bd_ProductReviewDe WHERE FProjectID IN (3, 4, 5, 6)) AS a
            INNER JOIN 
            dbo.Bd_Project AS b ON b.FProjectID = a.FProjectID
            WHERE 1=1
            ) AS x
            GROUP BY 
            x.FProductReviewID
            ) AS y ON y.评审ID = d.FProductReviewID

            WHERE 1=1
            AND a.FProductEntryID = '{FMID}'
            ";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                try
                {
                    connection.Open();
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            // 示例：只读取前几行并展示结果
                            int count = 0;

                            while (reader.Read())

                            {
                                // 创建新行
                                this.Model.CreateNewEntryRow("FEntity");
                                this.Model.SetValue("FOrderID", ObjectUtils.Object2String(reader["FOrderID"]), count);
                                this.Model.SetValue("FBillCode", ObjectUtils.Object2String(reader["FBillCode"]), count);
                                this.Model.SetValue("FWarrantyName", ObjectUtils.Object2String(reader["FWarrantyName"]), count);
                                this.Model.SetValue("FWorktaskManageID", ObjectUtils.Object2String(reader["FWorktaskManageID"]), count);
                                this.Model.SetValue("FProApplicationDeID", ObjectUtils.Object2String(reader["FProApplicationDeID"]), count);
                                this.Model.SetValue("FProductEntryCode", ObjectUtils.Object2String(reader["FProductEntryCode"]), count);
                                this.Model.SetValue("FProductReviewID", ObjectUtils.Object2String(reader["FProductReviewID"]), count);
                                this.Model.SetValue("FCode", ObjectUtils.Object2String(reader["FCode"]), count);
                                this.Model.SetValue("FProductEntryID", ObjectUtils.Object2String(reader["FProductEntryID"]), count);
                                this.Model.SetValue("FLCCard", ObjectUtils.Object2String(reader["FLCCard"]), count);
                                this.Model.SetValue("FStatus", ObjectUtils.Object2String(reader["FStatus"]), count);
                                this.Model.SetValue("FYCNum", ObjectUtils.Object2String(reader["FYCNum"]), count);
                                this.Model.SetValue("FLJRKNum", ObjectUtils.Object2String(reader["FLJRKNum"]), count);
                                this.Model.SetValue("FDPNum", ObjectUtils.Object2String(reader["FDPNum"]), count);
                                this.Model.SetValue("FUseID", ObjectUtils.Object2String(reader["FUseID"]), count);
                                this.Model.SetValue("FUseName", ObjectUtils.Object2String(reader["FUseName"]), count);
                                this.Model.SetValue("FUseCode", ObjectUtils.Object2String(reader["FUseCode"]), count);
                                this.Model.SetValue("FBatchNo", ObjectUtils.Object2String(reader["FBatchNo"]), count);
                                this.Model.SetValue("FTol", ObjectUtils.Object2String(reader["FTol"]), count);
                                this.Model.SetValue("FTotNo", ObjectUtils.Object2String(reader["FTotNo"]), count);
                                this.Model.SetValue("FTolNum", ObjectUtils.Object2String(reader["FTolNum"]), count);
                                this.Model.SetValue("FTotN", ObjectUtils.Object2String(reader["FTotN"]), count);
                                this.Model.SetValue("FTotNum", ObjectUtils.Object2String(reader["FTotNum"]), count);
                                this.Model.SetValue("FActualThickness", ObjectUtils.Object2String(reader["FActualThickness"]), count);
                                this.Model.SetValue("FActualWidth", ObjectUtils.Object2String(reader["FActualWidth"]), count);
                                this.Model.SetValue("FOrderNum", ObjectUtils.Object2String(reader["FOrderNum"]), count);
                                this.Model.SetValue("FStandardYield", ObjectUtils.Object2String(reader["FStandardYield"]), count);
                                this.Model.SetValue("FActualYield", ObjectUtils.Object2String(reader["FActualYield"]), count);
                                this.Model.SetValue("FApplyNum", ObjectUtils.Object2String(reader["FApplyNum"]), count);
                                this.Model.SetValue("FProductNum", ObjectUtils.Object2String(reader["FProductNum"]), count);
                                this.Model.SetValue("FDepotID", ObjectUtils.Object2String(reader["FDepotID"]), count);
                                this.Model.SetValue("FDepotCode", ObjectUtils.Object2String(reader["FDepotCode"]), count);
                                this.Model.SetValue("FDepotName", ObjectUtils.Object2String(reader["FDepotName"]), count);
                                this.Model.SetValue("FWorkShipName", ObjectUtils.Object2String(reader["FWorkShipName"]), count);
                                this.Model.SetValue("FRollNum", ObjectUtils.Object2String(reader["FRollNum"]), count);
                                this.Model.SetValue("FRemark_s", ObjectUtils.Object2String(reader["FRemark_s"]), count);
                                this.Model.SetValue("FWorkShipID", ObjectUtils.Object2String(reader["FWorkShipID"]), count);
                                this.Model.SetValue("FContractDeID", ObjectUtils.Object2String(reader["FContractDeID"]), count);
                                this.Model.SetValue("FContractCodes", ObjectUtils.Object2String(reader["FContractCodes"]), count);
                                this.Model.SetValue("FProductSpec", ObjectUtils.Object2String(reader["FProductSpec"]), count);
                                this.Model.SetValue("FProductID", ObjectUtils.Object2String(reader["FProductID"]), count);
                                this.Model.SetValue("FProductCode", ObjectUtils.Object2String(reader["FProductCode"]), count);
                                this.Model.SetValue("FProductName", ObjectUtils.Object2String(reader["FProductName"]), count);
                                this.Model.SetValue("FMaterialQualityID", ObjectUtils.Object2String(reader["FMaterialQualityID"]), count);
                                this.Model.SetValue("FMaterialQualityCode", ObjectUtils.Object2String(reader["FMaterialQualityCode"]), count);
                                this.Model.SetValue("FMaterialQualityNames", ObjectUtils.Object2String(reader["FMaterialQualityNames"]), count);
                                this.Model.SetValue("FUnitID", ObjectUtils.Object2String(reader["FUnitID"]), count);
                                this.Model.SetValue("FEmplID", ObjectUtils.Object2String(reader["FEmplID"]), count);
                                this.Model.SetValue("FEmplCode", ObjectUtils.Object2String(reader["FEmplCode"]), count);
                                this.Model.SetValue("FEmplName", ObjectUtils.Object2String(reader["FEmplName"]), count);
                                this.Model.SetValue("FThickness", ObjectUtils.Object2String(reader["FThickness"]), count);
                                this.Model.SetValue("FThicknessUpper", ObjectUtils.Object2String(reader["FThicknessUpper"]), count);
                                this.Model.SetValue("FThicknessLower", ObjectUtils.Object2String(reader["FThicknessLower"]), count);
                                this.Model.SetValue("FWidth", ObjectUtils.Object2String(reader["FWidth"]), count);
                                this.Model.SetValue("FWidthUpper", ObjectUtils.Object2String(reader["FWidthUpper"]), count);
                                this.Model.SetValue("FWidthLower", ObjectUtils.Object2String(reader["FWidthLower"]), count);
                                this.Model.SetValue("FLength", ObjectUtils.Object2String(reader["FLength"]), count);
                                this.Model.SetValue("FLengthUpper", ObjectUtils.Object2String(reader["FLengthUpper"]), count);
                                this.Model.SetValue("FLengthLower", ObjectUtils.Object2String(reader["FLengthLower"]), count);
                                this.Model.SetValue("FTargetThickness", ObjectUtils.Object2String(reader["FTargetThickness"]), count);
                                this.Model.SetValue("FWoodenModeID", ObjectUtils.Object2String(reader["FWoodenModeID"]), count);
                                this.Model.SetValue("FWoodenModeNames", ObjectUtils.Object2String(reader["FWoodenModeNames"]), count);
                                this.Model.SetValue("FDate", ObjectUtils.Object2String(reader["FDate"]), count);
                                this.Model.SetValue("FSumNum", ObjectUtils.Object2String(reader["FSumNum"]), count);
                                this.Model.SetValue("FRollNums", ObjectUtils.Object2String(reader["FRollNums"]), count);
                                this.Model.SetValue("FTotNums", ObjectUtils.Object2String(reader["FTotNums"]), count);
                                this.Model.SetValue("FWorkShipID_S", ObjectUtils.Object2String(reader["FWorkShipID_S"]), count);
                                this.Model.SetValue("FRemark", ObjectUtils.Object2String(reader["FRemark"]), count);
                                this.Model.SetValue("FCustomerID", ObjectUtils.Object2String(reader["FCustomerID"]), count);
                                this.Model.SetValue("FCustomerCodes", ObjectUtils.Object2String(reader["FCustomerCodes"]), count);
                                this.Model.SetValue("FCustomerNames", ObjectUtils.Object2String(reader["FCustomerNames"]), count);
                                this.Model.SetValue("FJHDates", ObjectUtils.Object2String(reader["FJHDates"]), count);
                                this.Model.SetValue("FMaterialQualityDeNames", ObjectUtils.Object2String(reader["FMaterialQualityDeNames"]), count);
                                this.Model.SetValue("FCreateDate", ObjectUtils.Object2String(reader["FCreateDate"]), count);
                                this.Model.SetValue("FCreater", ObjectUtils.Object2String(reader["FCreater"]), count);
                                this.Model.SetValue("FOperDate", ObjectUtils.Object2String(reader["FOperDate"]), count);
                                this.Model.SetValue("FOperUser", ObjectUtils.Object2String(reader["FOperUser"]), count);
                                this.Model.SetValue("FApprove", ObjectUtils.Object2String(reader["FApprove"]), count);
                                this.Model.SetValue("FApproveDate", ObjectUtils.Object2String(reader["FApproveDate"]), count);
                                this.Model.SetValue("FApprover", ObjectUtils.Object2String(reader["FApprover"]), count);
                                this.Model.SetValue("FDepaID", ObjectUtils.Object2String(reader["FDepaID"]), count);
                                this.Model.SetValue("FApproveDepaID", ObjectUtils.Object2String(reader["FApproveDepaID"]), count);
                                this.Model.SetValue("FIsApproval", ObjectUtils.Object2String(reader["FIsApproval"]), count);
                                this.Model.SetValue("FApprovalStauts", ObjectUtils.Object2String(reader["FApprovalStauts"]), count);
                                this.Model.SetValue("FApprovalDate", ObjectUtils.Object2String(reader["FApprovalDate"]), count);
                                this.Model.SetValue("FApplyEmplID", ObjectUtils.Object2String(reader["FApplyEmplID"]), count);
                                this.Model.SetValue("FEnable", ObjectUtils.Object2String(reader["FEnable"]), count);
                                this.Model.SetValue("FPreciseUpper", ObjectUtils.Object2String(reader["FPreciseUpper"]), count);
                                this.Model.SetValue("FPreciseLower", ObjectUtils.Object2String(reader["FPreciseLower"]), count);
                                this.Model.SetValue("FPreciseWeightUpper", ObjectUtils.Object2String(reader["FPreciseWeightUpper"]), count);
                                this.Model.SetValue("FPreciseWeightLower", ObjectUtils.Object2String(reader["FPreciseWeightLower"]), count);

                                count++;
                            }

                        }
                    }
                }
                catch (Exception ex)
                {
                    // 显示错误信息
                    this.View.ShowMessage("数据库访问出错: " + ex.Message);
                }
            }








        }
    }
}














namespace buttonOnclick
{
    [Description("按钮点击事件"), HotUpdate]
    public class buttonOnclick : AbstractDynamicFormPlugIn
    {

        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);

            if (e.BarItemKey.Equals("tb_open"))
            {

                DynamicFormShowParameter formPa = new DynamicFormShowParameter();
                //调用哪个表单
                formPa.FormId = "k131892645fa84a08bda00ffcd50320f5";
                this.View.ShowForm(formPa, delegate (FormResult result)
                {

                    DynamicObjectCollection returnData = (DynamicObjectCollection)result.ReturnData;
                    if (returnData != null)
                    {

                        //删除多余行数据
                        Entity entity = this.View.BusinessInfo.GetEntity("FEntity");
                        DynamicObjectCollection entityDataObjects1 = this.View.Model.GetEntityDataObject(entity);

                        // 记录要删除的行索引
                        List<int> rowsToDelete = new List<int>();

                        for (int j = 0; j < entityDataObjects1.Count; j++)
                        {
                            DynamicObject entityDataObject = entityDataObjects1[j];
                            rowsToDelete.Add(j);
                            //if (entityDataObject["F_FCustomerID"] == null)
                            //{
                            //    rowsToDelete.Add(j); // 将需要删除的行索引添加到列表
                            //}
                        }
                        // 从后往前删除行，以避免索引问题
                        for (int i = rowsToDelete.Count - 1; i >= 0; i--)
                        {
                            this.View.Model.DeleteEntryRow("FEntity", rowsToDelete[i]);
                        }


                        foreach (DynamicObject obj in returnData)
                        {
                            string FProApplicationDeID = ObjectUtils.Object2String(obj["FProApplicationDeID"]);
                            string sql = $@"select * from St_ProductEntryDe where FProApplicationDeID='{FProApplicationDeID}'";
                            var sqlData = DBUtils.ExecuteDynamicObject(this.Context, sql);
                            if (sqlData.Count > 0)
                            {
                                this.View.ShowMessage("{ " + ObjectUtils.Object2String(obj["FProductEntryCode"]) + "  }入库单号已经存在，请勿重复添加！");
                                return;
                            }
                        }
                        int count = 0;
                        foreach (DynamicObject obj in returnData)
                        {
                            this.View.Model.CreateNewEntryRow("FEntity");
                            this.View.Model.SetValue("FOrderID", ObjectUtils.Object2String(obj["FOrderID"]), count);
                            this.View.Model.SetValue("FBillCode", ObjectUtils.Object2String(obj["FBillCode"]), count);

                            this.View.Model.SetValue("FWarrantyName", ObjectUtils.Object2String(obj["FWarrantyName"]), count);
                            this.View.Model.SetValue("FWorktaskManageID", ObjectUtils.Object2String(obj["FWorktaskManageID"]), count);
                            this.View.Model.SetValue("FProApplicationDeID", ObjectUtils.Object2String(obj["FProApplicationDeID"]), count);
                            this.View.Model.SetValue("FProductEntryCode", ObjectUtils.Object2String(obj["FProductEntryCode"]), count);
                            this.View.Model.SetValue("FProductReviewID", ObjectUtils.Object2String(obj["FProductReviewID"]), count);
                            this.View.Model.SetValue("FCode", ObjectUtils.Object2String(obj["FCode"]), count);
                            this.View.Model.SetValue("FProductEntryID", ObjectUtils.Object2String(obj["FProductEntryID"]), count);
                            this.View.Model.SetValue("FLCCard", ObjectUtils.Object2String(obj["FLCCard"]), count);
                            this.View.Model.SetValue("FStatus", ObjectUtils.Object2String(obj["FStatus"]), count);
                            this.View.Model.SetValue("FYCNum", ObjectUtils.Object2String(obj["FYCNum"]), count);
                            this.View.Model.SetValue("FLJRKNum", ObjectUtils.Object2String(obj["FLJRKNum"]), count);
                            this.View.Model.SetValue("FDPNum", ObjectUtils.Object2String(obj["FDPNum"]), count);
                            this.View.Model.SetValue("FUseID", ObjectUtils.Object2String(obj["FUseID"]), count);
                            this.View.Model.SetValue("FUseName", ObjectUtils.Object2String(obj["FUseName"]), count);
                            this.View.Model.SetValue("FUseCode", ObjectUtils.Object2String(obj["FUseCode"]), count);


                            //用途
                            int FUse = YT(ObjectUtils.Object2String(obj["FUseCode"]));
                            this.View.Model.SetValue("FUse", FUse, count);


                            this.View.Model.SetValue("FBatchNo", ObjectUtils.Object2String(obj["FBatchNo"]), count);
                            this.View.Model.SetValue("FTol", ObjectUtils.Object2String(obj["FTol"]), count);
                            this.View.Model.SetValue("FTotNo", ObjectUtils.Object2String(obj["FTotNo"]), count);
                            this.View.Model.SetValue("FTolNum", ObjectUtils.Object2String(obj["FTolNum"]), count);
                            this.View.Model.SetValue("FTotN", ObjectUtils.Object2String(obj["FTotN"]), count);
                            this.View.Model.SetValue("FTotNum", ObjectUtils.Object2String(obj["FTotNum"]), count);
                            this.View.Model.SetValue("FActualThickness", ObjectUtils.Object2String(obj["FActualThickness"]), count);

                            this.View.Model.SetValue("FActualWidth", ObjectUtils.Object2String(obj["FActualWidth"]), count);
                            this.View.Model.SetValue("FOrderNum", ObjectUtils.Object2String(obj["FOrderNum"]), count);
                            this.View.Model.SetValue("FStandardYield", ObjectUtils.Object2String(obj["FStandardYield"]), count);
                            this.View.Model.SetValue("FActualYield", ObjectUtils.Object2String(obj["FActualYield"]), count);
                            this.View.Model.SetValue("FApplyNum", ObjectUtils.Object2String(obj["FApplyNum"]), count);
                            this.View.Model.SetValue("FProductNum", ObjectUtils.Object2String(obj["FProductNum"]), count);
                            //库位 -- 对不上
                            this.View.Model.SetValue("FDepotID", ObjectUtils.Object2String(obj["FDepotID"]), count);
                            this.View.Model.SetValue("FDepotName", ObjectUtils.Object2String(obj["FDepotName"]), count);
                            this.View.Model.SetValue("FDepotCode", ObjectUtils.Object2String(obj["FDepotCode"]), count);
                            this.View.Model.SetValue("FTargetThickness", ObjectUtils.Object2String(obj["FTargetThickness"]), count);
                            this.View.Model.SetValue("FWorkShipName", ObjectUtils.Object2String(obj["FWorkShipName"]), count);


                            this.View.Model.SetValue("FRollNum", ObjectUtils.Object2String(obj["FRollNum"]), count);
                            this.View.Model.SetValue("FRemark_s", ObjectUtils.Object2String(obj["FRemark_s"]), count);

                            //仓库
                            int FWorkShipID = CK(ObjectUtils.Object2Int(obj["FWorkShipID"]));
                            this.View.Model.SetValue("FWorkShipID", FWorkShipID, count);


                            this.View.Model.SetValue("FContractDeID", ObjectUtils.Object2String(obj["FContractDeID"]), count);
                            this.View.Model.SetValue("FContractCodes", ObjectUtils.Object2String(obj["FContractCodes"]), count);
                            this.View.Model.SetValue("FProductSpec", ObjectUtils.Object2String(obj["FProductSpec"]), count);
                            this.View.Model.SetValue("FProductID", ObjectUtils.Object2String(obj["FProductID"]), count);
                            this.View.Model.SetValue("FProductCode", ObjectUtils.Object2String(obj["FProductCode"]), count);
                            this.View.Model.SetValue("FProductName", ObjectUtils.Object2String(obj["FProductName"]), count);
                            //物料
                            //this.View.ShowMessage(ObjectUtils.Object2String(obj["FProductCode"]));
                            int FProduct = FMATERIALID(ObjectUtils.Object2String(obj["FContractCodes"]));
                            this.View.Model.SetValue("FProduct", FProduct, count);


                            this.View.Model.SetValue("FMaterialQualityID", ObjectUtils.Object2String(obj["FMaterialQualityID"]), count);
                            this.View.Model.SetValue("FMaterialQualityNames", ObjectUtils.Object2String(obj["FMaterialQualityNames"]), count);
                            this.View.Model.SetValue("FMaterialQualityCode", ObjectUtils.Object2String(obj["FMaterialQualityCode"]), count);
                            //材质
                            int FMaterialQuality = CZ(ObjectUtils.Object2String(obj["FMaterialQualityCode"]));
                            this.View.Model.SetValue("FMaterialQuality", FMaterialQuality, count);
                            this.View.Model.SetValue("FUnitID", ObjectUtils.Object2String(obj["FUnitID"]), count);

                            //单位
                            int FUnit = FUNITID(ObjectUtils.Object2Int(obj["FUnitID"]));
                            this.View.Model.SetValue("FUnit", FUnit, count);


                            this.View.Model.SetValue("FEmplID", ObjectUtils.Object2String(obj["FEmplID"]), count);
                            this.View.Model.SetValue("FEmplCode", ObjectUtils.Object2String(obj["FEmplCode"]), count);
                            this.View.Model.SetValue("FEmplName", ObjectUtils.Object2String(obj["FEmplName"]), count);

                            //业务员
                            int FEmpl = FENTRYID(ObjectUtils.Object2String(obj["FEmplCode"]));
                            this.View.Model.SetValue("FEmpl", FEmpl, count);

                            this.View.Model.SetValue("FThickness", ObjectUtils.Object2String(obj["FThickness"]), count);
                            this.View.Model.SetValue("FThicknessUpper", ObjectUtils.Object2String(obj["FThicknessUpper"]), count);
                            this.View.Model.SetValue("FThicknessLower", ObjectUtils.Object2String(obj["FThicknessLower"]), count);
                            this.View.Model.SetValue("FWidth", ObjectUtils.Object2String(obj["FWidth"]), count);
                            this.View.Model.SetValue("FWidthUpper", ObjectUtils.Object2String(obj["FWidthUpper"]), count);
                            this.View.Model.SetValue("FWidthLower", ObjectUtils.Object2String(obj["FWidthLower"]), count);
                            this.View.Model.SetValue("FLength", ObjectUtils.Object2String(obj["FLength"]), count);
                            this.View.Model.SetValue("FLengthUpper", ObjectUtils.Object2String(obj["FLengthUpper"]), count);
                            this.View.Model.SetValue("FLengthLower", ObjectUtils.Object2String(obj["FLengthLower"]), count);
                            this.View.Model.SetValue("FTargetThickness", ObjectUtils.Object2String(obj["FTargetThickness"]), count);
                            this.View.Model.SetValue("FWoodenModeID", ObjectUtils.Object2String(obj["FWoodenModeID"]), count);
                            this.View.Model.SetValue("FWoodenModeNames", ObjectUtils.Object2String(obj["FWoodenModeNames"]), count);
                            //木架 --对应不上
                            //this.View.Model.SetValue("FWoodenMode", ObjectUtils.Object2String(obj["FWoodenMode"]), count);

                            this.View.Model.SetValue("FDate", ObjectUtils.Object2String(obj["FDate"]), count);
                            this.View.Model.SetValue("FSumNum", ObjectUtils.Object2String(obj["FSumNum"]), count);
                            this.View.Model.SetValue("FRollNums", ObjectUtils.Object2String(obj["FRollNums"]), count);
                            this.View.Model.SetValue("FTotNums", ObjectUtils.Object2String(obj["FTotNums"]), count);
                            this.View.Model.SetValue("FWorkShipID_S", ObjectUtils.Object2String(obj["FWorkShipID_S"]), count);

                            //仓库
                            int FWorkShip_S = CK(ObjectUtils.Object2Int(obj["FWorkShipID_S"]));
                            this.View.Model.SetValue("FWorkShip_S", FWorkShip_S, count);


                            this.View.Model.SetValue("FRemark", ObjectUtils.Object2String(obj["FRemark"]), count);
                            this.View.Model.SetValue("FCustomerID", ObjectUtils.Object2String(obj["FCustomerID"]), count);
                            this.View.Model.SetValue("FCustomerCodes", ObjectUtils.Object2String(obj["FCustomerCodes"]), count);
                            this.View.Model.SetValue("FCustomerNames", ObjectUtils.Object2String(obj["FCustomerNames"]), count);
                            //客户
                            int FCustomer = FCUSTID(ObjectUtils.Object2String(obj["FCustomerCodes"]));
                            this.View.Model.SetValue("FCustomer", FCustomer, count);

                            this.View.Model.SetValue("FJHDates", ObjectUtils.Object2String(obj["FJHDates"]), count);
                            this.View.Model.SetValue("FMaterialQualityDeNames", ObjectUtils.Object2String(obj["FMaterialQualityDeNames"]), count);
                            this.View.Model.SetValue("FCreateDate", ObjectUtils.Object2String(obj["FCreateDate"]), count);
                            //创建人
                            int FCreater = FUSERID(ObjectUtils.Object2String(obj["FCreater"]));
                            this.View.Model.SetValue("FCreater", ObjectUtils.Object2String(FCreater), count);

                            this.View.Model.SetValue("FOperDate", ObjectUtils.Object2String(obj["FOperDate"]), count);
                            //操作人
                            int FOperator = FUSERID(ObjectUtils.Object2String(obj["FOperUser"]));
                            this.View.Model.SetValue("FOperator", ObjectUtils.Object2String(FOperator), count);

                            this.View.Model.SetValue("FApprove", ObjectUtils.Object2String(obj["FApprove"]), count);
                            this.View.Model.SetValue("FApproveDate", ObjectUtils.Object2String(obj["FApproveDate"]), count);
                            //审核人
                            int FApprover = FUSERID(ObjectUtils.Object2String(obj["FApprover"]));
                            this.View.Model.SetValue("FApprover", FApprover, count);
                            this.View.Model.SetValue("FDepaID", ObjectUtils.Object2String(obj["FDepaID"]), count);
                            this.View.Model.SetValue("FApproveDepaID", ObjectUtils.Object2String(obj["FApproveDepaID"]), count);
                            this.View.Model.SetValue("FIsApproval", ObjectUtils.Object2String(obj["FIsApproval"]), count);
                            this.View.Model.SetValue("FApprovalStauts", ObjectUtils.Object2String(obj["FApprovalStauts"]), count);
                            this.View.Model.SetValue("FApprovalDate", ObjectUtils.Object2String(obj["FApprovalDate"]), count);
                            this.View.Model.SetValue("FApplyEmplID", ObjectUtils.Object2String(obj["FApplyEmplID"]), count);
                            this.View.Model.SetValue("FEnable", ObjectUtils.Object2String(obj["FEnable"]), count);
                            this.View.Model.SetValue("FPreciseUpper", ObjectUtils.Object2String(obj["FPreciseUpper"]), count);
                            this.View.Model.SetValue("FPreciseLower", ObjectUtils.Object2String(obj["FPreciseLower"]), count);
                            this.View.Model.SetValue("FPreciseWeightUpper", ObjectUtils.Object2String(obj["FPreciseWeightUpper"]), count);
                            this.View.Model.SetValue("FPreciseWeightLower", ObjectUtils.Object2String(obj["FPreciseWeightLower"]), count);

                            count++;


                        }

                    }
                });
            }

        }
        //用户：极望账号匹配金蝶登录账号
        public int FUSERID(string FUSERID)
        {
            string query = $@"SELECT TOP 1 FUSERID FROM T_SEC_USER WHERE FUSERACCOUNT = '{FUSERID}'";
            var FBDCUSTID = DBUtils.ExecuteDynamicObject(this.Context, query);
            if (FBDCUSTID.Count > 0)
            {
                return ObjectUtils.Object2Int(FBDCUSTID[0]["FUSERID"]);
            }
            else
            {
                return 0;
            }
        }
        //合同类型匹配
        public String HTLX(int HTLX)
        {
            switch (HTLX)
            {
                case 13:
                    return "正式预售";
                case 14:
                    return "正式预售";
                case 17:
                    return "客户来料";
                case 22:
                    return "二级料";
                case 23:
                    return "废料";
                case 26:
                    return "客户备料";
                case 27:
                    return "针管料备料";
                case 30:
                    return "正式普通";
                case 32: //战略
                    return "";
                case 34:
                    return "原料贸易";
                case 51:
                    return "成品贸易";

            }
            return "";
        }
        //客户：极望客户编号匹配金蝶极望客户编码，限制为101用户组织

        public int FCUSTID(string FCUSTID)
        {
            string query = $@"
            select FCUSTID from dbo.T_BD_CUSTOMER where F_JWNUMBER = '{FCUSTID}'
            AND FUseOrgId = '100082'
            ";
            var FBDCUSTID = DBUtils.ExecuteDynamicObject(this.Context, query);
            if (FBDCUSTID.Count > 0)
            {
                return ObjectUtils.Object2Int(FBDCUSTID[0]["FCUSTID"]);
            }
            return 0;

        }
        //业务员：极望业务员编号匹配金蝶业务员编码，限制为101用户组织
        public int FENTRYID(string FUSERACCOUNT)
        {
            string sqlGetDeptId = $@"
            select 
            O.FENTRYID, 
            S.FSTAFFID, 
            S_L.FNAME, 
            P.FPERSONID, 
            U.FUSERID, 
            U.FNAME AS FNAME1, 
            U.FUSERACCOUNT
            from 
            T_SEC_USER U
            inner join 
            t_BD_Person P ON (u.FLINKOBJECT = P.FPERSONID)
            inner join 
            T_BD_STAFF S ON (P.FPERSONID = S.FPERSONID)
            left join 
            T_BD_STAFF_L S_L ON (S.FSTAFFID = S_L.FSTAFFID AND S_L.FLOCALEID = 2052)
            inner join 
            T_BD_OPERATORENTRY O ON (O.FSTAFFID = S.FSTAFFID AND O.FOPERATORTYPE = 'XSY')
            where U.FUSERACCOUNT = '{FUSERACCOUNT}'";
            var FBDCUSTID = DBUtils.ExecuteDynamicObject(this.Context, sqlGetDeptId);
            if (FBDCUSTID.Count > 0)
            {
                return ObjectUtils.Object2Int(FBDCUSTID[0]["FENTRYID"]);
            }
            return 0;

        }
        //单位：6-->吨
        // 5-->千克
        public int FUNITID(int FUNITID)
        {
            switch (FUNITID)
            {
                case 6:
                    return 10096;
                case 5:
                    return 10095;
            }
            return 0;
        }
        //仓库
        public int CK(int FWorkShipID)
        {
            switch (FWorkShipID)
            {
                case 32:
                    return 278557; //客户来料
                case 34:
                    return 265175; //成品库
                case 43:
                    return 265175; //成品库
                case 44:
                    return 265175; //成品库

            }
            return 265175;
        }
        //物料
        public int FMATERIALID(string F_UNW_JWFNumber)
        {
            //string query = $@"
            //select FMATERIALID from dbo.T_BD_MATERIAL where F_UNW_JWFNumber = '{F_UNW_JWFNumber}'
            //AND FUseOrgId = '100082'";
            string query = $@"
            select a.FMATERIALID,b.FBASEUNITID from dbo.T_BD_MATERIAL a
            INNER JOIN t_BD_MaterialBase b ON a.FMATERIALID =b.FMATERIALID
            where a.F_UNW_JWFNumber = '{F_UNW_JWFNumber}'
            AND a.FUseOrgId = '100082'";
            var FBDCUSTID = DBUtils.ExecuteDynamicObject(this.Context, query);
            if (FBDCUSTID.Count > 0)
            {
                return ObjectUtils.Object2Int(FBDCUSTID[0]["FMATERIALID"]);
            }
            return 0;

        }
        //物料计量单位
        public int FBASEUNITID(string F_UNW_JWFNumber)
        {
            //string query = $@"
            //select FMATERIALID from dbo.T_BD_MATERIAL where F_UNW_JWFNumber = '{F_UNW_JWFNumber}'
            //AND FUseOrgId = '100082'";
            string query = $@"
            select a.FMATERIALID,b.FBASEUNITID from dbo.T_BD_MATERIAL a
            INNER JOIN t_BD_MaterialBase b ON a.FMATERIALID =b.FMATERIALID
            where a.F_UNW_JWFNumber = '{F_UNW_JWFNumber}'
            AND a.FUseOrgId = '100082'";
            var FBDCUSTID = DBUtils.ExecuteDynamicObject(this.Context, query);
            if (FBDCUSTID.Count > 0)
            {
                return ObjectUtils.Object2Int(FBDCUSTID[0]["FBASEUNITID"]);
            }
            return 0;

        }
        //货币类型
        public int FCURRENCYID(int FCURRENCYID)
        {
            switch (FCURRENCYID)
            {
                case 19: //人民币
                    return 1;
                case 14://台币
                    return 5;
                case 16: //美元
                    return 7;
                case 21://越南币
                    return 272152;
                case 33: //英镑
                    return 6;
                case 37: //欧元
                    return 3;
            }
            return 0;

        }
        //材质,极望编号和金蝶编号匹配
        public int CZ(string FNUMBER)
        {
            string query = $@"
            select FID from dbo.Material_De where FNUMBER = '{FNUMBER}'";

            var FBDCUSTID = DBUtils.ExecuteDynamicObject(this.Context, query);
            if (FBDCUSTID.Count > 0)
            {
                return ObjectUtils.Object2Int(FBDCUSTID[0]["FID"]);
            }
            return 0;
        }
        //用途
        public int YT(string FNUMBER)
        {
            string query = $@"
            select FID from dbo.Usage_D where FNumber = '{FNUMBER}'";
            var FBDCUSTID = DBUtils.ExecuteDynamicObject(this.Context, query);
            if (FBDCUSTID.Count > 0)
            {
                return ObjectUtils.Object2Int(FBDCUSTID[0]["FID"]);
            }
            return 0;

        }
        //产地
        public int CD(string FNUMBER)
        {
            string query = $@"
             select FID from dbo.UNW_t_Cust100004 where FNumber = '{FNUMBER}'";
            var FBDCUSTID = DBUtils.ExecuteDynamicObject(this.Context, query);
            if (FBDCUSTID.Count > 0)
            {
                return ObjectUtils.Object2Int(FBDCUSTID[0]["FID"]);
            }
            return 0;

        }
        //执行标准
        public int ZXBZ(string FNUMBER)
        {
            string query = $@"
            SELECT FID FROM EX_De WHERE  FNumber = '{FNUMBER}'";
            var FBDCUSTID = DBUtils.ExecuteDynamicObject(this.Context, query);
            if (FBDCUSTID.Count > 0)
            {
                return ObjectUtils.Object2Int(FBDCUSTID[0]["FID"]);
            }
            return 0;

        }
        //牌号
        public int PH(string FNUMBER)
        {
            string query = $@"
            SELECT FID FROM Brand_Number WHERE  FNumber = '{FNUMBER}'";
            var FBDCUSTID = DBUtils.ExecuteDynamicObject(this.Context, query);
            if (FBDCUSTID.Count > 0)
            {
                return ObjectUtils.Object2Int(FBDCUSTID[0]["FID"]);
            }
            return 0;

        }
    }
}
//保存校验
namespace Kingdee.Bos.Test.ServicePlugln
{
    [Description("保存校验，是否存在销售订单")]
    [Kingdee.BOS.Util.HotUpdate]

    //继承操作服务插件
    public class OnAddValidatorsHead : AbstractBillPlugIn
    {

        public override void BeforeSave(BeforeSaveEventArgs e)
        {
            base.BeforeSave(e);

            // 获取单据体
            Entity entity = this.View.BillBusinessInfo.GetEntity("FEntity");
            DynamicObjectCollection entityDataObject = this.View.Model.GetEntityDataObject(entity);
            foreach (DynamicObject obj in entityDataObject)
            {
                //string FOrderID    = ObjectUtils.Object2String(obj["FOrderID"]);
                string FBillCode = ObjectUtils.Object2String(obj["FBillCode"]);
                string sql = $@"SELECT  F_JWOrderNumber FROM T_SAL_ORDERENTRY WHERE F_JWOrderNumber = '{FBillCode}'";
                //this.View.ShowMessage(sql);

                var reader = DBUtils.ExecuteDynamicObject(this.Context, sql);
                if (reader.Count == 0)
                {
                    e.Cancel = true;
                    this.View.ShowMessage("销售订单号:" + "'" + FBillCode + "'不存在，请先添加对应的销售订单!");
                }




            }





        }
    }
}